//
//  SEPrescriptionAPI.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/4/21.
//

import Moya

enum SEPrescriptionAPI {
    case listOfPrescription(inquiryId: String)
    case detailOfPrescription(id: String)
    case listOfPrescriptionDrug(content: String, pageNum: Int, pageSize: Int)
    case listOfDiagnosisPrefixAndSuffix(mark: String)
    case listOfDiagnosisResult(content: String, pageNum: Int, pageSize: Int)
    case listOfPrescriptionRejected(pageNum: Int, pageSize: Int)
    
    case detailOfPrescriptionDrug(id: String)
    
    case savePrescription(bisType: Int = 2, bisId: String, id: String, isChild: Int, proposal: String, remark: String, list: [[String: Any]], detailVos: [[String: Any]])
    case changePrescriptionStatus(id: String, status: Int)
    /// 处方模版列表
    case listOfPrescriptionTemplate(name: String = "", pageNum: Int, pageSize: Int)
    /// 处方模版详情
    case detailOfPrescriptionTemplate(id: String)
    /// 保存处方模版
    case savePrescriptionTemplate(id: String, name: String, isChild: Int, proposal: String, remark: String, diagnoseTemp: [[String: Any]], detailTemp: [[String: Any]])
    /// 删除处方模板
    case deletePrescriptionTemplate(id: String)
    
}

extension SEPrescriptionAPI: TargetType {
    var baseURL: URL { return URL(string: SEApiManager.baseUrl)! }
    
    var path: String {
        switch self {
        case .listOfPrescription: return "/smarteye/smarteye-inquiry/prescription/queryPrescriptionDtoByBisId"
        case .detailOfPrescription: return "/smarteye/smarteye-inquiry/prescription/getPrescriptionDto"
        case .listOfPrescriptionDrug: return "/smarteye/smarteye-inquiry/dict/queryMedicineDtoPageInfo"
        case .listOfDiagnosisPrefixAndSuffix: return "ng/newfuture-org/dict/listByMark"
        case .listOfDiagnosisResult: return "/smarteye/smarteye-inquiry/dict/queryIcd10DtoPageInfo"
        case .listOfPrescriptionRejected: return "/smarteye/smarteye-inquiry/prescription/queryPrescriptionDtoPage"
        case .detailOfPrescriptionDrug: return "/smarteye/smarteye-inquiry/dict/getMedicineDetailDtoById"
        case .savePrescription: return "/smarteye/smarteye-inquiry/prescription/savePrescriptionVo"
        case .changePrescriptionStatus: return "/smarteye/smarteye-inquiry/prescription/updatePrescriptionStatus"
            
        case .listOfPrescriptionTemplate: return "/smarteye/smarteye-inquiry/temp/queryPrescriptionTempDto"
        case .detailOfPrescriptionTemplate: return "/smarteye/smarteye-inquiry/temp/getPrescriptionTempDtoById"
        case .savePrescriptionTemplate: return "/smarteye/smarteye-inquiry/temp/savePrescriptionTempDto"
        case .deletePrescriptionTemplate: return "/smarteye/smarteye-inquiry/temp/deletePrescriptionTempDtoById"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .listOfPrescription,
             .detailOfPrescription,
             .listOfDiagnosisPrefixAndSuffix,
             .detailOfPrescriptionDrug,
             .detailOfPrescriptionTemplate,
             .deletePrescriptionTemplate:
            return .get
        case .listOfPrescriptionDrug,
             .listOfDiagnosisResult,
             .listOfPrescriptionRejected,
             .savePrescription,
             .changePrescriptionStatus,
             .listOfPrescriptionTemplate,
             .savePrescriptionTemplate:
            return .post
        }
    }
    
    var sampleData: Data { return "".data(using: .utf8)! }
    
    var task: Task {
        SELog("\(path) - \(parameters)")
        switch self {
        case .listOfPrescription,
             .detailOfPrescription,
             .listOfDiagnosisPrefixAndSuffix,
             .detailOfPrescriptionDrug,
             .detailOfPrescriptionTemplate,
             .deletePrescriptionTemplate:
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case .listOfPrescriptionDrug,
             .listOfDiagnosisResult,
             .listOfPrescriptionRejected,
             .savePrescription,
             .changePrescriptionStatus,
             .listOfPrescriptionTemplate,
             .savePrescriptionTemplate:
            return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
        }
    }
    
    var headers: [String : String]? {
        return SEApiManager.headers
    }
    
    private var parameters: [String: Any] {
        switch self {
        case .listOfPrescription(let inquiryId):
            return ["bisId": inquiryId]
        case let .listOfPrescriptionDrug(content, pageNum, pageSize),
             let .listOfDiagnosisResult(content, pageNum, pageSize):
            return ["content": content, "pageNum": pageNum, "pageSize": pageSize]
        case .listOfDiagnosisPrefixAndSuffix(let mark):
            return ["mark": mark]
        case .detailOfPrescription(let id),
             .detailOfPrescriptionDrug(let id),
             .detailOfPrescriptionTemplate(let id):
            return ["id": id]
        case let .savePrescription(bisType, bisId, id, isChild, proposal, remark, list, detailVos):
            return ["bisType": bisType, "bisId": bisId, "id": id, "isChild": isChild, "proposal": proposal, "remark": remark, "list": list, "detailVos": detailVos]
        case let .changePrescriptionStatus(id, status):
            return ["id": id, "status": status]
        case let .listOfPrescriptionRejected(pageNum, pageSize):
            return ["source": 3, "statusList": [2], "pageNum": pageNum, "pageSize": pageSize]
        case let .listOfPrescriptionTemplate(name, pageNum, pageSize):
            return ["name": name, "pageNum": pageNum, "pageSize": pageSize]
        case let .savePrescriptionTemplate(id, name, isChild, proposal, remark, diagnoseTemp, detailTemp):
            return ["id": id, "name": name, "isChild": isChild, "proposal": proposal, "remark": remark, "diagnoseTemp": diagnoseTemp, "detailTemp": detailTemp]
        case let .deletePrescriptionTemplate(id):
            return ["id": id]
        }
    }
}
